package cn.zmall.front.controller;

import cn.zmall.common.pojo.Result;
import cn.zmall.common.utils.ResultUtil;
import cn.zmall.manager.pojo.TbAddress;
import cn.zmall.sso.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author cxy
 * @Date 2020/11/12 19:05
 * @Version 1.0
 */
@RestController
@Api(value = "用户地址")
public class AddressController {

    @Reference
    private AddressService addressService;

    @RequestMapping(value = "/item/getaddress",method = RequestMethod.GET)
    @ApiOperation(value = "通过地址ID获取信息")
    public Result<TbAddress> getAddress(Long addressId){
        TbAddress result = addressService.getAddress(addressId);
        return new ResultUtil<TbAddress>().setData(result);
    }

    @RequestMapping(value = "/item/getlistaddress",method = RequestMethod.GET)
    @ApiOperation(value = "通过用户ID获取所有信息")
    public Result<List<TbAddress>> getListAddress(Long userId){
        List<TbAddress> result = addressService.getListAddress(userId);
        return new ResultUtil<List<TbAddress>>().setData(result);
    }

    @RequestMapping(value = "/item/addaddress",method = RequestMethod.POST)
    @ApiOperation(value = "添加地址")
    public Result<Object> addAddress(@RequestBody TbAddress tbAddress){
        int result = addressService.addAddress(tbAddress);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/deladdress",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除地址")
    public Result<Object> delAddress(Long addressId){
        int result = addressService.delAddress(addressId);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/updateaddress",method = RequestMethod.POST)
    @ApiOperation(value = "更新地址")
    public Result<Object> updateAddress(@RequestBody TbAddress tbAddress){
        int result = addressService.updateAddress(tbAddress);
        return new ResultUtil<Object>().setData(result);
    }
}
